using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;

namespace Xeno_Race.Helpers
{
    public class InputHelper
    {
        private KeyboardState _KeyboardState, _prevKeyboardState;
        private GamePadState _GamePadState, _prevGamePadState;
        private PlayerIndex _PlayerIndex = PlayerIndex.One;
        private Keys A, B, X, Y; 
        
        public static InputHelper GetState(PlayerIndex playerIndex)
        {
            InputHelper i = new InputHelper();
            i._PlayerIndex = playerIndex;
            i._prevKeyboardState = i._KeyboardState;
            i._prevGamePadState = i._GamePadState;
            i._GamePadState = GamePad.GetState(playerIndex);
            i._KeyboardState = Keyboard.GetState(playerIndex);
            i.A = Keys.V;
            i.B = Keys.B;
            i.X = Keys.N;
            i.Y = Keys.M;
            
            return i;
        }

        public static InputHelper GetState()
        {
            return GetState(PlayerIndex.One);
        }

        public void ResetState()
        {
            _KeyboardState = new KeyboardState();
            _GamePadState = new GamePadState();
        }



        public bool UpButton
        {
            get
            {
                if (_KeyboardState.IsKeyDown(Keys.W) || _KeyboardState.IsKeyDown(Keys.Up) || _GamePadState.DPad.Up == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool UpButtonJustPressed
        {
            get
            {//_KeyboardState.UpButton == ButtonState.Pressed && _PreviousInputState.UpButton == ButtonState.Released
                if (_KeyboardState.IsKeyDown(Keys.W) || _KeyboardState.IsKeyDown(Keys.Up) || _GamePadState.DPad.Up == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool DownButton
        {
            get
            {
                if (_KeyboardState.IsKeyDown(Keys.S) || _KeyboardState.IsKeyDown(Keys.Down) || _GamePadState.DPad.Down == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool LeftButton
        {
            get
            {
                if (_KeyboardState.IsKeyDown(Keys.A) || _KeyboardState.IsKeyDown(Keys.Left) || _GamePadState.DPad.Left == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool RightButton
        {
            get
            {
                if (_KeyboardState.IsKeyDown(Keys.D) || _KeyboardState.IsKeyDown(Keys.Right) || _GamePadState.DPad.Right == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool StartButton
        {
            get
            {
                if (_KeyboardState.IsKeyDown(Keys.Enter) || _GamePadState.Buttons.Start == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool SelectButton
        {
            get
            {
                if (_KeyboardState.IsKeyDown(Keys.Enter) || _GamePadState.Buttons.Start == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool ExitButton
        {
            get
            {
                if (_KeyboardState.IsKeyDown(Keys.Escape) || _GamePadState.Buttons.Back == ButtonState.Pressed)
                    return true;
                return false;
            }
        }

        public bool AButton
        {
            get
            {
                if (_GamePadState.Buttons.A == ButtonState.Pressed || _KeyboardState.IsKeyDown(A))
                    return true;
                return false;
            }
        }

        public bool BButton
        {
            get
            {
                if (_GamePadState.Buttons.B == ButtonState.Pressed || _KeyboardState.IsKeyDown(B))
                    return true;
                return false;
            }
        }

        public bool XButton
        {
            get
            {
                if (_GamePadState.Buttons.X == ButtonState.Pressed || _KeyboardState.IsKeyDown(X))
                    return true;
                return false;
            }
        }

        public bool YButton
        {
            get
            {
                if (_GamePadState.Buttons.Y == ButtonState.Pressed || _KeyboardState.IsKeyDown(Y))
                    return true;
                return false;
            }
        }

        public override bool Equals(object obj)
        {
            if(obj is InputHelper)
            {
                InputHelper o = obj as InputHelper;
                return this._PlayerIndex == o._PlayerIndex 
                        && this.DownButton == o.DownButton
                        && this.ExitButton == o.ExitButton
                        && this.LeftButton == o.LeftButton
                        && this.RightButton == o.RightButton
                        && this.StartButton == o.StartButton
                        && this.UpButton == o.UpButton;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}